/* 
 * File:   main.cpp
 * Author: brady
 *
 * Created on January 25, 2011, 2:13 PM
 */

#include <iostream>

#include "Construct.h"
#include <fstream>
#include <sstream>
#include <handleArgs.h>
#include <PNGImage.h>
#include <Vector3D.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "Scene.h"
#include "Sphere.h"
#include "Triangle.h"
#include "Shader.h"
#include "LambertianShader.h"
#include "BlinnPhongShader.h"
#include "Mesh.h"
#include "DielectricShader.h"
#include "SceneParser.h"
#include "TransformMatrix.h"

using namespace cs5721;
using namespace std;
/*
 * 
 */
void buildShader(fstream &str, Shape* shape);
void buildShape(fstream &str, Scene* scene);

int main(int argc, char** argv) {
    GraphicsArgs args;

    //set default arg vals before parsing. excluded args will take default.
    //this is useful for debugging as args aren't needed

    string scenesFolder = "cs5721raytracer/Scenes/";
    args.preSample = true;
    args.rpp = 16;
    args.numCpus = 4;
    args.width = 800;
    args.height = 800;
    args.inputFileName = scenesFolder+"Lense.rt";
    args.outputFileName = args.inputFileName + ".png";
    args.process(argc, argv);

    SceneParser parser(args.inputFileName);
    Scene s = parser.Parse();
    
        s.preSampling = args.preSample;
        s.BGColor = Vector3D(0.0, 0.0, 0.0);
        s.SamplesPerPixel = args.rpp;
        s.preSampling = args.preSample;
        
        
        cout << "Rendering file: "<< args.inputFileName << endl;
        cout << "Resolution: " << args.width << " x " << args.height << endl;
        cout << "Shapes: " << s.shapes.size() << endl;
        cout << "Recursion depth: " << s.RDepth << endl;
        cout << "Threads: " << args.numCpus <<endl;
        cout << "Samples per pixel: " << args.rpp << endl;
        cout << "Pre-sampling: " << s.preSampling<<endl;

    string suffix = "";
    string fname = args.outputFileName;
    for(int c = 0; c < s.cameras.size(); c++){
        if(c > 0){
            suffix = "";
            stringstream sufstr;
            sufstr << c;
            sufstr >> suffix;
            suffix = "_" + suffix;
            fname = args.outputFileName;
            fname.insert(args.outputFileName.size() - 4, suffix);
        }


        s.cameras[c]->SetAspectRatio((double)args.width/(double)args.height);
        s.CaptureImageMT(fname, s.cameras[c], args.width, args.height, args.numCpus);
        cout << "Image saved to "<< args.outputFileName <<endl;
    }
}
